Micron Document
`:top
In `F33f`_`[computer programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_programming]`_`f, a `F33f`_`[variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_(computer_science)]`_`f is said to be `*`!volatile`!`* if its `F33f`_`[value`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Value_(computer_science)]`_`f can be read or modified asynchronously by something other than the current `F33f`_`[thread of execution`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread_(computing)]`_`f. The value of a `B100`F9d9volatile`f`b variable may spontaneously change for reasons such as: sharing values with other threads; sharing values with asynchronous `F33f`_`[signal handlers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Signal_handler]`_`f; accessing hardware devices via `F33f`_`[memory-mapped I/O`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory-mapped_I/O]`_`f (where you can send and receive messages from `F33f`_`[peripheral devices`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Peripheral_device]`_`f by reading from and writing to memory). Support for these use cases varies considerably among the programming languages that have the `B100`F9d9volatile`f`b keyword. Volatility can have implications regarding function `F33f`_`[calling conventions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Calling_convention]`_`f and how variables are stored, accessed and cached.

>>Contents

• `F0af`_`[In C and C++`#in-c-and-c]`_`f
• `F0af`_`[Multi-threading`#multi-threading]`_`f
• `F0af`_`[Example of memory-mapped I/O in C`#example-of-memory-mapped-i-o-in-c]`_`f
• `F0af`_`[Optimization comparison in C`#optimization-comparison-in-c]`_`f
• `F0af`_`[Standards defects`#standards-defects]`_`f
• `F0af`_`[Compiler defects`#compiler-defects]`_`f
• `F0af`_`[In Java`#in-java]`_`f
• `F0af`_`[Early versions of Java`#early-versions-of-java]`_`f
• `F0af`_`[In C#`#in-c]`_`f
• `F0af`_`[In Fortran`#in-fortran]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f

-─

>>In C and C++

In C and C++, `B100`F9d9volatile`f`b is a `F33f`_`[type qualifier`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_qualifier]`_`f, like `B100`F9d9`F33f`_`[const`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Const_(computer_programming)]`_`f`f`b, and is a part of a `F33f`_`[type`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data_type]`_`f (e.g. the type of a variable or field).

The behavior of the `B100`F9d9volatile`f`b keyword in C and C++ is sometimes given in terms of suppressing optimizations of an `F33f`_`[optimizing compiler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Optimizing_compiler]`_`f: 1- don't remove existing `B100`F9d9volatile`f`b reads and writes, 2- don't add new `B100`F9d9volatile`f`b reads and writes, and 3- don't reorder `B100`F9d9volatile`f`b reads and writes. However, this definition is only an approximation for the benefit of new learners, and this approximate definition should not be relied upon to write real production code.

In C, and consequently C++, the `B100`F9d9volatile`f`b keyword was intended to:`:cite-ref-auto-1-0[`F5bf`_`[1`#cite-note-auto-1]`_`f]

• Allow access to `F33f`_`[memory-mapped I/O`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory-mapped_I/O]`_`f devices.
• Allow preserving values across a `B100`F9d9`F33f`_`[longjmp`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Setjmp]`_`f`f`b.
• Allow sharing values between signal handlers and the rest of the program in `B100`F9d9volatile`f`b `B100`F9d9sig_atomic_t`f`b objects.

The C and C++ standards allow writing portable code that shares values across a `B100`F9d9`F33f`_`[longjmp`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Setjmp]`_`f`f`b in `B100`F9d9volatile`f`b objects, and the standards allow writing portable code that shares values between signal handlers and the rest of the code in `B100`F9d9volatile`f`b `B100`F9d9sig_atomic_t`f`b objects. Any other use of `B100`F9d9volatile`f`b keyword in C and C++ is inherently non-portable or incorrect. In particular, writing code with the `B100`F9d9volatile`f`b keyword for `F33f`_`[memory-mapped I/O`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory-mapped_I/O]`_`f devices is inherently non-portable and always requires deep knowledge of the specific target C/C++ implementation and platform.

>>>Multi-threading

It is a common misconception that the `B100`F9d9volatile`f`b keyword is useful in portable `F33f`_`[multi-threading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread_(computing)]`_`f code in C and C++. The `B100`F9d9volatile`f`b keyword in C and C++ has `*never`* functioned as a useful, portable tool for `*any`* multi-threading scenario.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f]`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f]`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f] Unlike the `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f and `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f programming languages, operations on `B100`F9d9volatile`f`b variables in C and C++ are not `F33f`_`[atomic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Atomic_operation]`_`f, and operations on `B100`F9d9volatile`f`b variables do not have sufficient `F33f`_`[memory ordering`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory_ordering]`_`f guarantees (i.e. `F33f`_`[memory barriers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory_barrier]`_`f). Most C and C++ compilers, linkers, and runtimes simply do not provide the necessary memory ordering guarantees to make the `B100`F9d9volatile`f`b keyword useful for `*any`* multi-threading scenario. Before the C11 and C++11 standards, programmers were forced to rely on guarantees from the individual implementations and platforms (e.g. POSIX and WIN32) to write `F33f`_`[multi-threading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread_(computing)]`_`f code. With the modern C11 and C++11 standards, programmers can write portable `F33f`_`[multi-threading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread_(computing)]`_`f code using new portable constructs such as the `B100`F9d9std::atomic<T>`f`b templates.`:cite-ref-6[`F5bf`_`[6`#cite-note-6]`_`f]

>>>Example of memory-mapped I/O in C

In this example, the code sets the value stored in `B100`F9d9foo`f`b to `B100`F9d90`f`b. It then starts to `F33f`_`[poll`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Polling_(computer_science)]`_`f that value repeatedly until it changes to `B100`F9d9255`f`b:

`B100`F9d9static int foo;`f`b
`B100`F9d9`f`b
`B100`F9d9void bar(void) {`f`b
`B100`F9d9 foo = 0;`f`b
`B100`F9d9`f`b
`B100`F9d9 while (foo != 255)`f`b
`B100`F9d9 ;`f`b
`B100`F9d9}`f`b

An `F33f`_`[optimizing compiler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Optimizing_compiler]`_`f will notice that no other code can possibly change the value stored in `B100`F9d9foo`f`b, and will assume that it will remain equal to `B100`F9d90`f`b at all times. The compiler will therefore replace the function body with an `F33f`_`[infinite loop`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Infinite_loop]`_`f similar to this:

`B100`F9d9void bar_optimized(void) {`f`b
`B100`F9d9 foo = 0;`f`b
`B100`F9d9`f`b
`B100`F9d9 while (true)`f`b
`B100`F9d9 ;`f`b
`B100`F9d9}`f`b

However, the programmer may make `B100`F9d9foo`f`b refer to another element of the computer system such as a `F33f`_`[hardware register`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Hardware_register]`_`f of a device connected to the `F33f`_`[CPU`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=CPU]`_`f which may change the value of `B100`F9d9foo`f`b while this code is running. (This example does not include the details on how to make `B100`F9d9foo`f`b refer to a hardware register of a device connected to the CPU.) Without the `B100`F9d9volatile`f`b keyword, an `F33f`_`[optimizing compiler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Optimizing_compiler]`_`f will likely convert the code from the first sample with the read in the loop to the second sample without the read in the loop as part of the common `F33f`_`[loop-invariant code-motion optimization`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Loop-invariant_code_motion]`_`f, and thus the code will likely never notice the change that it is waiting for.

To prevent the compiler from doing this optimization, the `B100`F9d9volatile`f`b keyword can be used:

`B100`F9d9static volatile int foo;`f`b
`B100`F9d9`f`b
`B100`F9d9void bar (void) {`f`b
`B100`F9d9 foo = 0;`f`b
`B100`F9d9`f`b
`B100`F9d9 while (foo != 255)`f`b
`B100`F9d9 ;`f`b
`B100`F9d9}`f`b

The `B100`F9d9volatile`f`b keyword prevents the compiler from moving the read out of the loop, and thus the code will notice the expected change to the variable `B100`F9d9foo`f`b.

>>>Optimization comparison in C

The following C programs, and accompanying assembler language excerpts, demonstrate how the `B100`F9d9volatile`f`b keyword affects the compiler's output. The compiler in this case was `F33f`_`[GCC`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GNU_Compiler_Collection]`_`f.

While observing the assembly code, it is clearly visible that the code generated with `B100`F9d9volatile`f`b objects is more verbose, making it longer so the nature of `B100`F9d9volatile`f`b objects can be fulfilled. The `B100`F9d9volatile`f`b keyword prevents the compiler from performing optimization on code involving volatile objects, thus ensuring that each volatile variable assignment and read has a corresponding memory access. Without the `B100`F9d9volatile`f`b keyword, the compiler knows a variable does not need to be reread from memory at each use, because there should not be any writes to its memory location from any other thread or process.

`t
| Assembly comparison | Assembly comparison |
|---|---|
| Without volatile keyword | With volatile keyword |
| # include <stdio.h> int main () { /* These variables will never be created on stack*/ int a = 10 , b = 100 , c = 0 , d = 0 ; /* "printf" will be called with arguments "%d" and 110 (the compiler computes the sum of a+b), hence no overhead of performing addition at run-time */ printf ( "%d" , a + b ); /* This code will be removed via optimization, but the impact of 'c' and 'd' becoming 100 can be seen while calling "printf" */ a = b ; c = b ; d = b ; /* Compiler will generate code where printf is called with arguments "%d" and 200 */ printf ( "%d" , c + d ); return 0 ; } | # include <stdio.h> int main () { volatile int a = 10 , b = 100 , c = 0 , d = 0 ; printf ( "%d" , a + b ); a = b ; c = b ; d = b ; printf ( "%d" , c + d ); return 0 ; } |
| gcc -S -O3 -masm=intel noVolatileVar.c -o without.s | gcc -S -O3 -masm=intel VolatileVar.c -o with.s |
| .file "noVolatileVar.c" .intel_syntax noprefix .section .rodata.str1.1 , "aMS" , @progbits , 1 .LC0: .string "%d" .section .text.startup , "ax" , @progbits .p2align 4 ,, 15 .globl main .type main , @function main: .LFB11: .cfi_startproc sub rsp , 8 .cfi_def_cfa_offset 16 mov esi , 110 mov edi , OFFSET FLAT :. LC0 xor eax , eax call printf mov esi , 200 mov edi , OFFSET FLAT :. LC0 xor eax , eax call printf xor eax , eax add rsp , 8 .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE11: .size main , .-main .ident "GCC: (GNU) 4.8.2" .section .note.GNU-stack , "" , @progbits | .file "VolatileVar.c" .intel_syntax noprefix .section .rodata.str1.1 , "aMS" , @progbits , 1 .LC0: .string "%d" .section .text.startup , "ax" , @progbits .p2align 4 ,, 15 .globl main .type main , @function main: .LFB11: .cfi_startproc sub rsp , 24 .cfi_def_cfa_offset 32 mov edi , OFFSET FLAT :. LC0 mov DWORD PTR [ rsp ], 10 mov DWORD PTR [ rsp + 4 ], 100 mov DWORD PTR [ rsp + 8 ], 0 mov DWORD PTR [ rsp + 12 ], 0 mov esi , DWORD PTR [ rsp ] mov eax , DWORD PTR [ rsp + 4 ] add esi , eax xor eax , eax call printf mov eax , DWORD PTR [ rsp + 4 ] mov edi , OFFSET FLAT :. LC0 mov DWORD PTR [ rsp ], eax mov eax , DWORD PTR [ rsp + 4 ] mov DWORD PTR [ rsp + 8 ], eax mov eax , DWORD PTR [ rsp + 4 ] mov DWORD PTR [ rsp + 12 ], eax mov esi , DWORD PTR [ rsp + 8 ] mov eax , DWORD PTR [ rsp + 12 ] add esi , eax xor eax , eax call printf xor eax , eax add rsp , 24 .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE11: .size main , .-main .ident "GCC: (GNU) 4.8.2" .section .note.GNU-stack , "" , @progbits |
`t

>>>Standards defects

While intended by both C and C++, the current C standard fails to express that the `B100`F9d9volatile`f`b semantics refer to the lvalue, not the referenced object. The respective defect report `*DR 476`* (to C11) is still under review with `F33f`_`[C17`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C17_(C_standard_revision)]`_`f.`:cite-ref-7[`F5bf`_`[7`#cite-note-7]`_`f]

>>>Compiler defects

Unlike other language features of C and C++, the `B100`F9d9volatile`f`b keyword is not well supported by most C/C++ implementations - even for portable uses according to the C and C++ standards. Most C/C++ implementations are buggy regarding the behavior of the `B100`F9d9volatile`f`b keyword.`:cite-ref-8[`F5bf`_`[8`#cite-note-8]`_`f]`:cite-ref-9[`F5bf`_`[9`#cite-note-9]`_`f] Programmers should take great care whenever using the `B100`F9d9volatile`f`b keyword in C and C++.

>>In Java

In all modern versions of the `F33f`_`[Java programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_programming_language]`_`f, the `B100`F9d9volatile`f`b keyword gives the following guarantees:

• `B100`F9d9volatile`f`b reads and writes are `F33f`_`[atomic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Atomic_operation]`_`f. In particular, reads and writes to `B100`F9d9long`f`b and `B100`F9d9double`f`b fields will not tear. (The `F33f`_`[atomic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Atomic_operation]`_`f guarantee applies only to the `B100`F9d9volatile`f`b primitive value or the `B100`F9d9volatile`f`b reference value, and `*not`* to any Object value.)
• There is a single global ordering of all `B100`F9d9volatile`f`b reads and writes. In other words, a `B100`F9d9volatile`f`b read will read the current value (and not a past or future value), and all `B100`F9d9volatile`f`b reads will agree on a single global order of `B100`F9d9volatile`f`b writes.
• `B100`F9d9volatile`f`b reads and writes have "acquire" and "release" `F33f`_`[memory barrier`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory_barrier]`_`f semantics (known in the Java standard as `F33f`_`[happens-before`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Happened-before]`_`f).`:cite-ref-10[`F5bf`_`[10`#cite-note-10]`_`f]`:cite-ref-11[`F5bf`_`[11`#cite-note-11]`_`f] In other words, `B100`F9d9volatile`f`b provides guarantees about the relative order of `B100`F9d9volatile`f`b and non-`B100`F9d9volatile`f`b reads and writes. In other words, `B100`F9d9volatile`f`b basically provides the same memory visibility guarantees as a Java `F33f`_`[synchronized block`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lock_(computer_science)]`_`f (but without the `F33f`_`[mutual exclusion`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mutual_exclusion]`_`f guarantees of a `F33f`_`[synchronized block`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lock_(computer_science)]`_`f).

Together, these guarantees make `B100`F9d9volatile`f`b into a useful `F33f`_`[multi-threading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread_(computing)]`_`f construct in `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_programming_language]`_`f. In particular, the typical `F33f`_`[double-checked locking`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Double-checked_locking]`_`f algorithm with `B100`F9d9volatile`f`b works correctly in `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_programming_language]`_`f.`:cite-ref-12[`F5bf`_`[12`#cite-note-12]`_`f]

>>>Early versions of Java

Before Java version 5, the Java standard did not guarantee the relative ordering of `B100`F9d9volatile`f`b and non-`B100`F9d9volatile`f`b reads and writes. In other words, `B100`F9d9volatile`f`b did not have "acquire" and "release" `F33f`_`[memory barrier`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory_barrier]`_`f semantics. This greatly limited its use as a `F33f`_`[multi-threading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread_(computing)]`_`f construct. In particular, the typical `F33f`_`[double-checked locking`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Double-checked_locking]`_`f algorithm with `B100`F9d9volatile`f`b did `*not`* work correctly.

>>In C#

In `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f, `B100`F9d9volatile`f`b ensures that code accessing the field is not subject to some thread-unsafe optimizations that may be performed by the compiler, the CLR, or by hardware. When a field is marked `B100`F9d9volatile`f`b, the compiler is instructed to generate a "memory barrier" or "fence" around it, which prevents instruction reordering or caching tied to the field. When reading a `B100`F9d9volatile`f`b field, the compiler generates an `*acquire-fence`*, which prevents other reads and writes to the field from being moved `*before`* the fence. When writing to a `B100`F9d9volatile`f`b field, the compiler generates a `*release-fence`*; this fence prevents other reads and writes to the field from being moved `*after`* the fence.`:cite-ref-albahari-13-0[`F5bf`_`[13`#cite-note-albahari-13]`_`f]

Only the following types can be marked `B100`F9d9volatile`f`b: all reference types, `B100`F9d9Single`f`b, `B100`F9d9Boolean`f`b, `B100`F9d9Byte`f`b, `B100`F9d9SByte`f`b, `B100`F9d9Int16`f`b, `B100`F9d9UInt16`f`b, `B100`F9d9Int32`f`b, `B100`F9d9UInt32`f`b, `B100`F9d9Char`f`b, and all enumerated types with an underlying type of `B100`F9d9Byte`f`b, `B100`F9d9SByte`f`b, `B100`F9d9Int16`f`b, `B100`F9d9UInt16`f`b, `B100`F9d9Int32`f`b, or `B100`F9d9UInt32`f`b.`:cite-ref-14[`F5bf`_`[14`#cite-note-14]`_`f] (This excludes value `F33f`_`[structs`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Struct]`_`f, as well as the primitive types `B100`F9d9Double`f`b, `B100`F9d9Int64`f`b, `B100`F9d9UInt64`f`b and `B100`F9d9Decimal`f`b.)

Using the `B100`F9d9volatile`f`b keyword does not support fields that are `F33f`_`[passed by reference`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Evaluation_strategy]`_`f or `F33f`_`[captured local variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Closure_(computer_programming)]`_`f; in these cases, `B100`F9d9Thread.VolatileRead`f`b and `B100`F9d9Thread.VolatileWrite`f`b must be used instead.`:cite-ref-albahari-13-1[`F5bf`_`[13`#cite-note-albahari-13]`_`f]

In effect, these methods disable some optimizations usually performed by the C# compiler, the JIT compiler, or the CPU itself. The guarantees provided by `B100`F9d9Thread.VolatileRead`f`b and `B100`F9d9Thread.VolatileWrite`f`b are a superset of the guarantees provided by the `B100`F9d9volatile`f`b keyword: instead of generating a "half fence" (ie an acquire-fence only prevents instruction reordering and caching that comes before it), `B100`F9d9VolatileRead`f`b and `B100`F9d9VolatileWrite`f`b generate a "full fence" which prevent instruction reordering and caching of that field in both directions.`:cite-ref-albahari-13-2[`F5bf`_`[13`#cite-note-albahari-13]`_`f] These methods work as follows:`:cite-ref-15[`F5bf`_`[15`#cite-note-15]`_`f]

• The `B100`F9d9Thread.VolatileWrite`f`b method forces the value in the field to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to `B100`F9d9VolatileWrite`f`b and any later program-order loads and stores must occur after the call.
• The `B100`F9d9Thread.VolatileRead`f`b method forces the value in the field to be read from at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to `B100`F9d9VolatileRead`f`b and any later program-order loads and stores must occur after the call.

The `B100`F9d9Thread.VolatileRead`f`b and `B100`F9d9Thread.VolatileWrite`f`b methods generate a full fence by calling the `B100`F9d9Thread.MemoryBarrier`f`b method, which constructs a memory barrier that works in both directions. In addition to the motivations for using a full fence given above, one potential problem with the `B100`F9d9volatile`f`b keyword that is solved by using a full fence generated by `B100`F9d9Thread.MemoryBarrier`f`b is as follows: due to the asymmetric nature of half fences, a `B100`F9d9volatile`f`b field with a write instruction followed by a read instruction may still have the execution order swapped by the compiler. Because full fences are symmetric, this is not a problem when using `B100`F9d9Thread.MemoryBarrier`f`b.`:cite-ref-albahari-13-3[`F5bf`_`[13`#cite-note-albahari-13]`_`f]

>>In Fortran

`B100`F9d9VOLATILE`f`b is part of the `F33f`_`[Fortran 2003`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Fortran]`_`f standard,`:cite-ref-16[`F5bf`_`[16`#cite-note-16]`_`f] although earlier version supported it as an extension. Making all variables `B100`F9d9volatile`f`b in a function is also useful finding `F33f`_`[aliasing`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Aliasing_(computing)]`_`f related bugs.

`B100`F9d9integer, volatile :: i ! When not defined volatile the following two lines of code are identical`f`b
`B100`F9d9write(*,*) i**2 ! Loads the variable i once from memory and multiplies that value times itself`f`b
`B100`F9d9write(*,*) i*i ! Loads the variable i twice from memory and multiplies those values`f`b

By always "drilling down" to memory of a VOLATILE, the Fortran compiler is precluded from reordering reads or writes to volatiles. This makes visible to other threads actions done in this thread, and vice versa.`:cite-ref-17[`F5bf`_`[17`#cite-note-17]`_`f]

Use of VOLATILE reduces and can even prevent optimization.`:cite-ref-18[`F5bf`_`[18`#cite-note-18]`_`f]

>>References

`:cite-note-auto-1`!1.`! `F0af`_`[↑`#cite-ref-auto-1-0]`_`f "Publication on C++ standards committee".
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "Volatile Keyword In Visual C++". `*Microsoft MSDN`*. 21 September 2021.
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f "Linux Kernel Documentation – Why the "volatile" type class should not be used". `*kernel.org`*.
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f `:citerefscott-meyersandrei-alexandrescu2004`aScott Meyers; Andrei Alexandrescu (2004). "C++ and the Perils of Double-Checked Locking" (PDF). `*DDJ`*.
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f `:citerefjeremy-andrews2007`aJeremy Andrews (2007). "Linux: Volatile Superstition". kerneltrap.org. Archived from the original on 2010-06-20. Retrieved Jan 9, 2011.
`:cite-note-6`!6.`! `F0af`_`[↑`#cite-ref-6]`_`f "volatile (C++)". `*Microsoft MSDN`*. 21 September 2021.
`:cite-note-7`!7.`! `F0af`_`[↑`#cite-ref-7]`_`f `*Clarification Request Summary for C11.`* Version 1.13, October 2017.
`:cite-note-8`!8.`! `F0af`_`[↑`#cite-ref-8]`_`f `:citerefeideregehr2008`aEide, Eric; Regehr, John (October 2008). "Volatiles Are Miscompiled, and What to Do about It" (PDF). `*Proceedings of the Eighth ACM and IEEE International Conference on Embedded Software (EMSOFT), Atlanta, Georgia, USA`* – via cs.utah.edu.
`:cite-note-9`!9.`! `F0af`_`[↑`#cite-ref-9]`_`f "Volatile Bugs, Three Years Later – Embedded in Academia". `*blog.regehr.org`*. Retrieved 2024-08-28.
`:cite-note-10`!10.`! `F0af`_`[↑`#cite-ref-10]`_`f Section 17.4.4: Synchronization Order "The Java® Language Specification, Java SE 7 Edition". `F33f`_`[Oracle Corporation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Oracle_Corporation]`_`f. 2013. Retrieved 2013-05-12.
`:cite-note-11`!11.`! `F0af`_`[↑`#cite-ref-11]`_`f "Java Concurrency: Understanding the 'Volatile' Keyword". dzone.com. 2021-03-08. Archived from the original on 2021-05-09. Retrieved 2021-05-09.
`:cite-note-12`!12.`! `F0af`_`[↑`#cite-ref-12]`_`f `:citerefneil-coffey`aNeil Coffey. "Double-checked Locking (DCL) and how to fix it". Javamex. Retrieved 2009-09-19.
`:cite-note-albahari-13`!13.`! `F0af`_`[↑`#cite-ref-albahari-13-0]`_`f `:citerefalbahari`aAlbahari, Joseph. "Part 4: Advanced Threading". `*Threading in C#`*. O'Reilly Media. Archived from the original on 12 December 2019. Retrieved 9 December 2019.`B100`F9d9{{cite web}}`f`b: CS1 maint: bot: original URL status unknown (link)
`:cite-note-14`!14.`! `F0af`_`[↑`#cite-ref-14]`_`f `:citerefrichter2010`aRichter, Jeffrey (February 11, 2010). "Chapter 7: Constants and Fields". `*CLR Via C#`*. Microsoft Press. pp. 183. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-0-7356-2704-8.
`:cite-note-15`!15.`! `F0af`_`[↑`#cite-ref-15]`_`f `:citerefrichter2010`aRichter, Jeffrey (February 11, 2010). "Chapter 28: Primitive Thread Synchronization Constructs". `*CLR Via C#`*. Microsoft Press. pp. 797–803. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-0-7356-2704-8.
`:cite-note-16`!16.`! `F0af`_`[↑`#cite-ref-16]`_`f "VOLATILE Attribute and Statement". Cray. Archived from the original on 2018-01-23. Retrieved 2016-04-22.
`:cite-note-17`!17.`! `F0af`_`[↑`#cite-ref-17]`_`f "Volatile and shared array in Fortran". `*Intel.com`*.
`:cite-note-18`!18.`! `F0af`_`[↑`#cite-ref-18]`_`f "VOLATILE". `*Oracle.com`*.

>>External links

• Ada Reference Manual C.6: Shared Variable Control
• Linux kernel: volatile-considered-harmful

`c`F0af`_`[↑ Back to top`#top]`_`f`a